/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import etcore.webapp.grouptalk.common.config.Config;

/**
 * @author ETCORE GroupTalk Developers
 * 
 */
public class DateUtil {
	public static void main(String[] args) {
		System.out.println(DateUtil.getTime(Config
				.getProperty(Config.HOURS_DIFFERENCE)));
	}

	public static Date getDateBefore(Date date, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
		return now.getTime();
	}

	public static Date getDateAfter(Date date, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);

		return now.getTime();
	}

	public static String getPreviousDate(String inputDate, int day) {

		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = dateFormat.parse(inputDate);

			Date result = getDateBefore(date, day);

			return dateFormat.format(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getPreviousDate(int day) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = new Date();
			Date result = getDateBefore(date, day);

			return dateFormat.format(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	public static String getDate() {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = new Date();

			return dateFormat.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	public static int getQuot(String time1, String time2) {
		int result = 0;
		SimpleDateFormat ft = new SimpleDateFormat("yyy-MM-dd");
		try {
			Date date1 = ft.parse(time1);
			Date date2 = ft.parse(time2);
			long quot = date1.getTime() - date2.getTime();
			result = (int) (quot / 1000 / 60 / 60 / 24);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String getNextDate(String inputDate, int day) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = dateFormat.parse(inputDate);

			Date result = getDateAfter(date, day);

			return dateFormat.format(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getNextDate(int day) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = new Date();
			Date result = getDateAfter(date, day);
			return dateFormat.format(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getLaterTime(String inputTime, int seconds) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		java.util.Date date1 = null;
		if (inputTime == null)
			inputTime = format.format(new Date());
		if (inputTime.trim().length() == 10)
			inputTime = inputTime + " 00:00:00";
		try {
			date1 = format.parse(inputTime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long Time = (date1.getTime() / 1000) + seconds;
		date1.setTime(Time * 1000);
		return format.format(date1);
	}

	public static String getTime(String correctedHours) {
		int corrected = 0;
		corrected = Integer.parseInt(correctedHours);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date date = new Date();

		long Time = (date.getTime() / 1000) + corrected * 3600;
		date.setTime(Time * 1000);
		return format.format(date);
	}
}
